//
//  WhyViewController.m
//  多线程
//
//  Created by Maggie on 14-8-28.
//  Copyright (c) 2014年 fmning. All rights reserved.
//

#import "WhyViewController.h"
#define kImageURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
#import "LoadingView.h"
@interface WhyViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;

@end

@implementation WhyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self downLoadImageUseGCD];
//    [self downLoadImageUsingThread];
//    [self downLoadImageUseOperationQueue];
}

#pragma mark - 在viewWillAppear之后调用，视图显示后调用
-(void)viewDidAppear:(BOOL)animated{
//    [self downLoadImageNonThread];
}


#pragma mark - 不使用多线程，直接请求图片数据
-(void)downLoadImageNonThread{
    
    NSString *urlPath = @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg";
    NSURL *url = [NSURL URLWithString:urlPath];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    sleep(3);
    UIImage *image = [[UIImage alloc]initWithData:data];
    
    
    self.imageView.image = image;
}

#pragma mark - 下载图像方法一：使用NSThread
-(void)downLoadImageUsingThread{
    NSString *url = @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg";
    NSThread *myThread = [[NSThread alloc]initWithTarget:self selector:@selector(downLoadImage:) object:url];
    [myThread start];
}

#pragma mark - 下载图像方法二：使用NSOperationQueue
- (void)downLoadImageUseOperationQueue
{
    //子类一：NSInvocationOperation
    NSString *url = @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg";
    NSInvocationOperation *operator = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage:) object:url];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:operator];
    
    //子类二：NSBlockOperation
    /*
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        sleep(3);
        //    [NSThread sleepForTimeInterval:3];
        NSURL *url = [NSURL URLWithString:kImageURL];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc]initWithData:data];
        
        //通知主线程更新UI
        [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:block];
     */
}
#pragma mark - 线程任务请求图片数据
- (void)downLoadImage:(NSString *)urlPath
{
    sleep(3);
//    [NSThread sleepForTimeInterval:3];
    NSURL *url = [NSURL URLWithString:urlPath];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc]initWithData:data];
    
    //通知主线程更新UI
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    
}
#pragma mark - 更新UI
- (void)updateUI:(UIImage *)image
{
    self.imageView.image = image;
}


#pragma mark - 下载图像方法三：使用GCD
- (void)downLoadImageUseGCD
{
    //加载自定义视图
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"LoadingView" owner:self options:nil];
    LoadingView *loading = nibs[0];
    loading.frame = CGRectMake(0, 0, 167, 85);
    loading.center = self.imageView.center;
    [self.view addSubview:loading];
    
    [loading.indicatorView startAnimating];
    
    //dispatch_async是GCD中创建新线程的方法
    //第一个参数：队列,获取的一个全局队列dispatch_get_global_queue
    //第二个参数：新的线程要做什么？采用block来进行任务传递
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(3);
        //定义你要什么
         NSString *urlPath = @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg";
        NSURL *url = [NSURL URLWithString:urlPath];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc]initWithData:data];
        
        //更新UI,通知主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = image;
            [loading.indicatorView stopAnimating];
            loading.statusLable.text = @"加载完毕!";
            
            [UIView animateWithDuration:1 animations:^{
                loading.alpha = 0;
            } completion:^(BOOL finished) {
                [loading removeFromSuperview];
            }];
            
        });
    });

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




